/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/
#include "zb32l03x_hal.h"

#include "FreeRTOS.h"
#include "queue.h"
#include "app_lcd.h"
#include "app_key.h"
#include "app_beep.h"
#include "app_powerctrl.h"
#include "Ringbuff.h"
#include "ui_lcd.h"
#include "log.h"
#include "tty.h"
#include "app_loop.h"
#include "math.h"

extern QueueHandle_t xQueueLoopSend;
/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/
void ui_lcd_init(ui_t *ui)
{
  static uint8_t uiringbuff[16];
  ERR_E err;
  uint8_t i;
  ui->curpageidx = UI_PAGE_IDX_INIT;
  ui->lcdlighttimer = xTaskGetTickCount();
  ui->lcdreflashtimer = xTaskGetTickCount();
  ui->powerofftimer = xTaskGetTickCount();
  ui->batterlevel = app_powerctrl_getbatterlevel();
  ui->poweronflag = 0;
  ui->autopoweroff = 1;
  ui->testmodecnt =xTaskGetTickCount();//进入自检模式30s有效
  err = RingBuff_Create(&ui->ring, uiringbuff, sizeof(uiringbuff));

  ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
}

void ui_lcd_postkeyval(ui_t *ui, ui_keyval_e keyval)
{
  ui_keyval_e buff[1];
  ERR_E err;
  buff[0] = keyval;
  RingBuff_Write(&ui->ring, (uint8_t *)buff, 1, &err);
  
}

ui_keyval_e ui_lcd_getkeyval(ui_t*ui)
{
   ui_keyval_e buff[1];
   ERR_E err ;
   if(RingBuff_Read(&ui->ring, (uint8_t *)buff, 1, &err))
   {
    return buff[0];
   }
   else
   {
     return UI_KEYVAL_NON; 
   }
   
}

bool ui_lcd_isrealkeyval(ui_keyval_e keyval)
{
   if (keyval < UI_KEYVAL_INIT_UI)
   {
     return true;
   }
   else
   {
     return false;
   }
}

void ui_lcd_showprogbar(uint8_t value, uint8_t showflag)
{
   char buff[16];
	 if (showflag)
   {
     sprintf(buff, "%03d%%", value);
     app_lcd_dispchar_str(95, 47, buff, 16, 1);
   }
   else
   {
     app_lcd_dispchar_str(95, 47, "    ", 16, 1);
   }
}

void ui_lcd_showshort(uint8_t shortflag)
{
	 if (shortflag)
   {
     app_lcd_dispchar_str(95, 47, "ERR", 16, 1);
   }
   else
   {
     app_lcd_dispchar_str(95, 47, "    ", 16, 1);
   }
}

void ui_lcd_showresult(uint8_t result)
{
   if (result == 1)
   {
     app_lcd_dispchar_str(95, 47, "    ", 16, 1);
     app_lcd_dispchar_ch(95, 47, 32, 32, 1);
   }
   else if (result == 0)
   {
     app_lcd_dispchar_str(95, 47, "    ", 16, 1);
     app_lcd_dispchar_ch(95, 47, 31, 32, 1);
   }
   else
   {
     app_lcd_dispchar_str(95, 47, "    ", 16, 1);
   }
}

void ui_lcd_showaddress(int add,uint8_t showflag)
{
   char buff[16];
   if(showflag)
   {
   sprintf(buff, "%03d", add);
   app_lcd_dispchar_str(40, 30, buff, 32, 1);
   }
   else
   {
    app_lcd_dispchar_str(40, 30, "   ", 32, 1);
   }
}

void ui_lcd_seladdresscur(int add, int sel)
{
   uint8_t seladd = 0;
   char buff[16];
   switch (sel)
   {
   case -1:
     ui_lcd_showaddress(add,1);
     break;
   case 0:               // 个
     seladd = add / 100; // 百
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(40, 30, buff, 32, 1);

     seladd = add % 100 / 10; // 十
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(56, 30, buff, 32, 1);

     seladd = add % 100 % 10; // 个
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(72, 30, buff, 32, 0);
     break;
   case 1:               // 十
     seladd = add / 100; // 百
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(40, 30, buff, 32, 1);

     seladd = add % 100 / 10; // 十
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(56, 30, buff, 32, 0);

     seladd = add % 100 % 10; // 个
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(72, 30, buff, 32, 1);
     break;

   case 2:               // 百
     seladd = add / 100; // 百
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(40, 30, buff, 32, 0);

     seladd = add % 100 / 10; // 十
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(56, 30, buff, 32, 1);

     seladd = add % 100 % 10; // 个
     sprintf(buff, "%01d", seladd);
     app_lcd_dispchar_str(72, 30, buff, 32, 1);

     break;
   default:
     break;
   }
}

void ui_lcd_selelectricalcur(int para, int sel)
{
   uint8_t selpara = 0;
   char buff[16];
   switch (sel)
   {
   case -1:
     sprintf(buff, "%04d", para);
     app_lcd_dispchar_str(30, 30, buff, 32, 1);
     break;
   case 0:                  // 个
     selpara = para / 1000; // 千
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(30, 30, buff, 32, 1);

     selpara = para % 1000 / 100; // 百
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(46, 30, buff, 32, 1);

     selpara = para % 100 / 10; // 十
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(62, 30, buff, 32, 1);

     selpara = para % 100 % 10; // 个
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(78, 30, buff, 32, 0);
     break;
   case 1:                  // 十
     selpara = para / 1000; // 千
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(30, 30, buff, 32, 1);

     selpara = para % 1000 / 100; // 百
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(46, 30, buff, 32, 1);

     selpara = para % 100 / 10; // 十
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(62, 30, buff, 32, 0);

     selpara = para % 100 % 10; // 个
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(78, 30, buff, 32, 1);
     break;

   case 2:                  // 百
     selpara = para / 1000; // 千
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(30, 30, buff, 32, 1);

     selpara = para % 1000 / 100; // 百
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(46, 30, buff, 32, 0);

     selpara = para % 100 / 10; // 十
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(62, 30, buff, 32, 1);

     selpara = para % 100 % 10; // 个
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(78, 30, buff, 32, 1);

     break;
   case 3:                  // 千
     selpara = para / 1000; // 千
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(30, 30, buff, 32, 0);

     selpara = para % 1000 / 100; // 百
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(46, 30, buff, 32, 1);

     selpara = para % 100 / 10; // 十
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(62, 30, buff, 32, 1);

     selpara = para % 100 % 10; // 个
     sprintf(buff, "%01d", selpara);
     app_lcd_dispchar_str(78, 30, buff, 32, 1);

     break;
   default:
     break;
   }
}

void ui_lcd_selmultifuncur(uint8_t subfun, uint16_t para, int cur)
{
   uint16_t sel = 0;
   char buff[16];
   switch (cur)
   {
   case -1:
    //子功能码 
     sprintf(buff, "%02X", subfun);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);
     //参数
     sprintf(buff, "%04X", para);
     app_lcd_dispchar_str(45, 42, buff, 16, 1);
     break;
   case 0: 
    //子功能码              
     sel = subfun / 16; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);

     sel = subfun % 16; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(73, 20, buff, 16, 0);

     //参数
     sprintf(buff, "%04X", para);
     app_lcd_dispchar_str(45, 42, buff, 16, 1);

     break;
   case 1:
     // 子功能码
     sel = subfun / 16;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(65, 20, buff, 16, 0);

     sel = subfun % 16;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(73, 20, buff, 16, 1);

     // 参数
     sprintf(buff, "%04X", para);
     app_lcd_dispchar_str(45, 42, buff, 16, 1);
     break;

   case 2:               
     //子功能码              
     sprintf(buff, "%02X", subfun);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);

     //参数
     sel = para / 0x1000; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(45, 42, buff, 16, 1);

     sel = para % 0x1000 / 0x100; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(53, 42, buff, 16, 1);

     sel = para % 0x1000 % 0x100 / 0x10; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(61, 42, buff, 16, 1);

     sel = para % 0x10 ; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(69, 42, buff, 16, 0);


     break;
   case 3:               
     //子功能码              
     sprintf(buff, "%02X", subfun);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);

     //参数
     sel = para / 0x1000; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(45, 42, buff, 16, 1);

     sel = para % 0x1000 / 0x100; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(53, 42, buff, 16, 1);

     sel = para % 0x1000 % 0x100 / 0x10; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(61, 42, buff, 16, 0);

     sel = para % 0x10 ; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(69, 42, buff, 16, 1);


     break;
  case 4:               
     //子功能码              
     sprintf(buff, "%02X", subfun);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);

     //参数
     sel = para / 0x1000; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(45,42, buff,16, 1);

     sel = para % 0x1000 / 0x100; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(53,42, buff, 16, 0);

     sel = para % 0x1000 % 0x100 / 0x10; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(61, 42, buff, 16, 1);

     sel = para % 0x10 ; 
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(69, 42, buff, 16, 1);


     break;
   case 5:
     // 子功能码
     sprintf(buff, "%02X", subfun);
     app_lcd_dispchar_str(65, 20, buff, 16, 1);

     // 参数
     sel = para / 0x1000;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(45, 42, buff, 16, 0);

     sel = para % 0x1000 / 0x100;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(53, 42, buff, 16, 1);

     sel = para % 0x1000 % 0x100 / 0x10;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(61, 42, buff, 16, 1);

     sel = para % 0x10;
     sprintf(buff, "%01X", sel);
     app_lcd_dispchar_str(69, 42, buff, 16, 1);
     break;
   default:
     break;
   }
}
void ui_lcd_page_idx_init(ui_t *ui, ui_keyval_e keyval)
{
   switch (keyval)
   {
   case UI_KEYVAL_POWER_LONG:
     ui->curpageidx = UI_PAGE_IDX_CODER;
     ui->poweronflag = 1;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     app_lcd_init();
     app_beep_powerontone();
     bsp_powerctrl_openvc(HAL_VC_INTERRUPT_TRIGGER_FALL);
     break;
   default:
    break;
   }
}

void ui_lcd_page_idx_coder(ui_t *ui, ui_keyval_e keyval)
{
   static int add,addbk;
   uint8_t buff[16];
   int i,j;
   int res;
   uint8_t progbar=0;
   int keynum=0;
   static loopmode_e mode = LOOPMODE_9000;
   static int keynumcursoridx;

   if (addbk != add)
   {
    ui_lcd_showresult(2);
    addbk = add;
   }

   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     
     app_lcd_clear();
     add = 324;
     addbk = add;
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<读码编码>
     app_lcd_dispchar_ch(20, 2, 0, 32, 1);
     app_lcd_dispchar_ch(36, 2, 1, 32, 1);
     app_lcd_dispchar_ch(52, 2, 2, 32, 1);
     app_lcd_dispchar_ch(68, 2, 3, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     ui_lcd_seladdresscur(add, ui->cursoridx);
     if (app_powerctrl_isexpower() == true) // 外置电源时打开总线
     {
       app_loop_open();
     }
     break;
   case UI_KEYVAL_POWER_SHORT:
     app_loop_close();
     ui->curpageidx = UI_PAGE_IDX_INC_CODER;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add < 324)
       {
         add++;
       }
       else
       {
         add = 1;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add <= 314)
       {
         add = add + 10;
       }
     }
     else
     {
       if (add <= 224)
       {
         add = add + 100;
       }
     }
     break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add > 1)
       {
         add--;
       }
       else
       {
         add = 324;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add > 10)
       {
         add = add - 10;
       }
     }
     else
     {
       if (add > 100)
       {
         add = add - 100;
       }
     }

     break;
   case UI_KEYVAL_WRITE_SHORT:
     progbar = 0;
     ui->cursoridx = -1;
     keynumcursoridx = 0;
     if ((add > 0) && (app_loop_open() == 1))
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 10; i++)
         {
           // 显示进度
           progbar = 5 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_writecode(mode, add);
           if (res == 2) // A58双地址
           {
             app_beep_successtone_1();
             ui_lcd_seladdresscur(add, ui->cursoridx);
             vTaskDelay(500);
             app_beep_successtone_1();
             add = add + 1;
             addbk = add;
             break;
           }
           else if (res == 1) // 单地址
           {
             app_beep_successtone_1();
             break;
           }
         }

         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           if (mode == LOOPMODE_9000)
           {
             mode = LOOPMODE_EPC;
           }
           else
           {
             mode = LOOPMODE_9000;
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       if (app_powerctrl_isexpower() == false) // 电池供电才关闭总线
       {
         vTaskDelay(500);
         app_loop_close();
       }
     }
     else
     {
       ui_lcd_showresult(0);
     }

     break;
   case UI_KEYVAL_READ_SHORT:
     progbar = 0;
     ui->cursoridx = -1;
     keynumcursoridx = 0;
     if (app_loop_open() == 1)
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 10; i++)
         {
           progbar = 5 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);

           res = app_loop_readcode(mode, &add);
           if (res == 2) // A58双地址
           {
             app_beep_successtone_1();
             ui_lcd_seladdresscur(add, ui->cursoridx);
             vTaskDelay(500);
             app_beep_successtone_1();
             add = add + 1;
             addbk = add;
             break;
           }
           else if (res == 1) // 单地址
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           if (mode == LOOPMODE_9000)
           {
             mode = LOOPMODE_EPC;
           }
           else
           {
             mode = LOOPMODE_9000;
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
     }
     else
     {
       ui_lcd_showresult(0);
     }
     if (app_powerctrl_isexpower() == false) // 电池供电才关闭总线
     {
       app_loop_close();
     }
     break;
   case UI_KEYVAL_SHORT_UI:
       ui_lcd_showshort(1);
       app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     if (app_powerctrl_isexpower() == false) // 电池供电
     {
       ui_lcd_showshort(0);
     }
     else
     {
       app_loop_open();
       ui_lcd_showshort(0);
     }
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     keynum = keyval - UI_KEYVAL_0_SHORT;
     if (ui->cursoridx == -1)
     {

       if (keynumcursoridx == 0)
       {
         add = 0;
       }
       if (keynumcursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         keynumcursoridx = 1;
       }
       else if (keynumcursoridx == 1)
       {
         i = (add % 10) * 10 + keynum;
         keynumcursoridx = 2;
       }
       else if (keynumcursoridx == 2)
       {
         i = add / 10 * 100 + add % 10 * 10 + keynum;

         keynumcursoridx = 0;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
       else
       {
         if ((ui->testmodecnt) && (i == 789))
         {
           ui->curpageidx = UI_PAGE_IDX_TESTMODE;
         }
         else
         {
           keynumcursoridx = 0;
         }
       }
     }
     else
     {
       keynumcursoridx = 0;
       if (ui->cursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         ui->cursoridx = 2;
       }
       else if (ui->cursoridx == 1)
       {
         i = (add / 100) * 100 + keynum * 10 + (add % 10);
         ui->cursoridx = ui->cursoridx - 1;
       }
       else if (ui->cursoridx == 2)
       {
         i = keynum * 100 + add % 100;
         ui->cursoridx = ui->cursoridx - 1;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
     }

     break;
   case UI_KEYVAL_LEFT_SHORT:
     if (ui->cursoridx < 2)
     {
       ui->cursoridx++;
     }
     else
     {
       ui->cursoridx = 0;
     }
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if (ui->cursoridx > 0)
     {
       ui->cursoridx--;
     }
     else
     {
       ui->cursoridx = 2;
     }
     break;
   case UI_KEYVAL_OK_LONG:
     ui->cursoridx = -1;
     if (app_loop_open() == 1)
     {
       vTaskDelay(200);
       for (i = 0; i < 5; i++)
       {
         res = app_loop_readcode(LOOPMODE_EPC, &add);
         if (res > 0)
         {
           break;
         }
         else
         {
           vTaskDelay(100);
         }
       }

       if (res)
       {
         app_loop_operatelamp(add, 1, 0);
         ui_lcd_showresult(1);
         app_beep_successtone_1();
         vTaskDelay(1000);
       }
       else
       {
         ui_lcd_showresult(0);
         app_beep_errortone();
       }
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
       app_beep_errortone();
     }

     break;
   default:
     break;
   }
   
   ui_lcd_seladdresscur(add, ui->cursoridx);
  
}

void ui_lcd_page_idx_inccoder(ui_t *ui, ui_keyval_e keyval)
{
   static int add;
   static uint8_t incflag;
   static uint8_t addflash;
   static TickType_t tickstart;
   static uint8_t removeflag=0;
   uint8_t buff[16];
   int i,j;
   int res;
   int keynum=0;
   int addtemp;
   static loopmode_e mode = LOOPMODE_9000;
   static int keynumcursoridx;
   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     add = 1;
     app_lcd_clear();
     incflag = 0;
     addflash = 0;
     removeflag = 0;
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<自增编码>
     app_lcd_dispchar_ch(20, 2, 4, 32, 1);
     app_lcd_dispchar_ch(36, 2, 5, 32, 1);
     app_lcd_dispchar_ch(52, 2, 6, 32, 1);
     app_lcd_dispchar_ch(68, 2, 7, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     break;
   case UI_KEYVAL_POWER_SHORT:
     if ((incflag & 0x01) == 0)
     {
       ui->curpageidx = UI_PAGE_IDX_MODELSET;
       ui_lcd_postkeyval(ui, UI_KEYVAL_INIT_UI);
     }
     break;
   case UI_KEYVAL_SHORT_UI:
     if (incflag & 0x01)
     {
       addflash = 0;
       ui_lcd_showaddress(add, 1);
       app_loop_close();
       incflag = 0;
     }
     ui_lcd_showshort(1);
     app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     ui_lcd_showshort(0);
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     if ((incflag & 0x01) == 0)
     {
       if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
       {
         if (add < 324)
         {
           add++;
         }
         else
         {
           add = 1;
         }
       }
       else if (ui->cursoridx == 1)
       {
         if (add <= 314)
         {
           add = add + 10;
         }
       }
       else
       {
         if (add <= 224)
         {
           add = add + 100;
         }
       }
     }
     break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     if ((incflag & 0x01) == 0)
     {
       if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
       {
         if (add > 1)
         {
           add--;
         }
         else
         {
           add = 324;
         }
       }
       else if (ui->cursoridx == 1)
       {
         if (add > 10)
         {
           add = add - 10;
         }
       }
       else
       {
         if (add > 100)
         {
           add = add - 100;
         }
       }
     }
     break;
   case UI_KEYVAL_OK_SHORT:
     if (app_powerctrl_isshortdet() == 0)
     {
       incflag++;
       if (incflag & 0x01)
       {
         app_loop_open();
         removeflag = 0;
         
       }
       else
       {
         addflash = 0;
         ui_lcd_showaddress(add, 1);
         app_loop_close();
       }
       ui_lcd_showshort(0);
     }
     else
     {
       ui_lcd_showshort(1);
       app_beep_errortone();
     }
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     break;
   case UI_KEYVAL_WRITE_SHORT:
     break;
   case UI_KEYVAL_READ_SHORT:
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     if ((incflag & 0x01) == 0)
     {
       keynum = keyval - UI_KEYVAL_0_SHORT;
       if (ui->cursoridx == -1)
       {
         if (keynumcursoridx == 0)
         {
           add = 0;
         }
         if (keynumcursoridx == 0)
         {
           i = (add / 10 * 10) + keynum;
           keynumcursoridx = 1;
         }
         else if (keynumcursoridx == 1)
         {
           i = (add % 10) * 10 + keynum;
           keynumcursoridx = 2;
         }
         else if (keynumcursoridx == 2)
         {
           i = add / 10 * 100 + add % 10 * 10 + keynum;

           keynumcursoridx = 0;
         }
         if ((i <= 324) && (i > 0))
         {
           add = i;
         }
       }
       else
       {
         keynumcursoridx = 0;
         if (ui->cursoridx == 0)
         {
           i = (add / 10 * 10) + keynum;
           ui->cursoridx = 2;
         }
         else if (ui->cursoridx == 1)
         {
           i = (add / 100) * 100 + keynum * 10 + (add % 10);
           ui->cursoridx = ui->cursoridx - 1;
         }
         else if (ui->cursoridx == 2)
         {
           i = keynum * 100 + add % 100;
           ui->cursoridx = ui->cursoridx - 1;
         }
         if ((i <= 324) && (i > 0))
         {
           add = i;
         }
       }
     }
     break;
   case UI_KEYVAL_LEFT_SHORT:
     if ((incflag & 0x01) == 0)
     {
       if (ui->cursoridx < 2)
       {
         ui->cursoridx++;
       }
       else
       {
         ui->cursoridx = 0;
       }
     }
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if ((incflag & 0x01) == 0)
     {
       if (ui->cursoridx > 0)
       {
         ui->cursoridx--;
       }
       else
       {
         ui->cursoridx = 2;
       }
     }
     break;
   default:
     break;
   }
   if (incflag & 0x01)
   {
     if (xTaskGetTickCount() - tickstart > 500)
     {
       tickstart = xTaskGetTickCount();
       addflash++;
       ui_lcd_showaddress(add, addflash & 0x01);
       //终端存在
       if (app_loop_readcode(mode, &addtemp))
       {
         // 可以编码
         if (removeflag == 0)
         {
           res = app_loop_writecode(mode, add);
           if (res)
           {
             add = (add + res) % 324;
             if (add == 0)
             {
               add = 324;
             }
             app_beep_successtone_1();
             //防止自动关机和灭屏
             app_lcd_setbllevel(1);
             ui->lcdlighttimer = xTaskGetTickCount();
             ui->powerofftimer = xTaskGetTickCount();
           }
           removeflag = 1;
         }
       }
       else
       {
         if (mode == LOOPMODE_9000)
         {
           mode = LOOPMODE_EPC;
         }
         else
         {
           mode = LOOPMODE_9000;
         }
         removeflag = 0;
       }
     }
     
   }
   else
   {
     ui_lcd_seladdresscur(add, ui->cursoridx);
   }
}

void ui_lcd_page_idx_model(ui_t *ui, ui_keyval_e keyval)
{
   static int add,addbk;
   uint8_t buff[16];
   int i,j;
   int res;
   uint8_t progbar=0;
   int keynum=0;
   static loopmode_e mode = LOOPMODE_9000;
   static int keynumcursoridx = 0;
   if (addbk != add)
   {
    ui_lcd_showresult(2);
    addbk = add;
   }
   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     add = 1;
     addbk = add;
     app_lcd_clear();
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<模块设置>
     app_lcd_dispchar_ch(20, 2, 8, 32, 1);
     app_lcd_dispchar_ch(36, 2, 9, 32, 1);
     app_lcd_dispchar_ch(52, 2, 10, 32, 1);
     app_lcd_dispchar_ch(68, 2, 11, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     break;
   case UI_KEYVAL_POWER_SHORT:
     ui->curpageidx = UI_PAGE_IDX_SOUNDERSET;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add < 324)
       {
         add++;
       }
       else
       {
         add = 1;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add <= 314)
       {
         add = add + 10;
       }
     }
     else
     {
       if (add <= 224)
       {
         add = add + 100;
       }
     }
     break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add > 1)
       {
         add--;
       }
       else
       {
         add = 324;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add > 10)
       {
         add = add - 10;
       }
     }
     else
     {
       if (add > 100)
       {
         add = add - 100;
       }
     }

     break;
   case UI_KEYVAL_WRITE_SHORT:
     progbar=0;
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     if ((add > 0) && (app_loop_open() == 1))
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           // 显示进度
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_modelmodeset(mode, add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       vTaskDelay(500);
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_READ_SHORT:
     progbar=0;
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     if (app_loop_open() == 1)
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);

           res = app_loop_modelmodeget(mode, &add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_SHORT_UI:
     ui_lcd_showshort(1);
     app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     ui_lcd_showshort(0);
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     keynum = keyval - UI_KEYVAL_0_SHORT;
     if (ui->cursoridx == -1)
     {
       if (keynumcursoridx == 0)
       {
         add = 0;
       }
       if (keynumcursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         keynumcursoridx = 1;
       }
       else if (keynumcursoridx == 1)
       {
         i = (add % 10) * 10 + keynum;
         keynumcursoridx = 2;
       }
       else if (keynumcursoridx == 2)
       {
         i = add / 10 * 100 + add % 10 * 10 + keynum;

         keynumcursoridx = 0;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
     }
     else
     {
       keynumcursoridx = 0;
       if (ui->cursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         ui->cursoridx = 2;
       }
       else if (ui->cursoridx == 1)
       {
         i = (add / 100) * 100 + keynum * 10 + (add % 10);
         ui->cursoridx = ui->cursoridx - 1;
       }
       else if (ui->cursoridx == 2)
       {
         i = keynum * 100 + add % 100;
         ui->cursoridx = ui->cursoridx - 1;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
     }
     break;
   case UI_KEYVAL_LEFT_SHORT:
     if (ui->cursoridx < 2)
     {
       ui->cursoridx++;
     }
     else
     {
       ui->cursoridx = 0;
     }
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if (ui->cursoridx > 0)
     {
       ui->cursoridx--;
     }
     else
     {
       ui->cursoridx = 2;
     }
     break;
   default:
     break;
   }
   ui_lcd_seladdresscur(add, ui->cursoridx);
}

void ui_lcd_page_idx_sounder(ui_t *ui, ui_keyval_e keyval)
{
   static int add,addbk;
   uint8_t buff[16];
   int i,j;
   int res;
   uint8_t progbar=0;
   int keynum=0;
   static loopmode_e mode = LOOPMODE_9000;
   static int keynumcursoridx;
   if (addbk != add)
   {
    ui_lcd_showresult(2);
    addbk = add;
   }

   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     add = 1;
     addbk = add;
     app_lcd_clear();
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<声光设置>
     app_lcd_dispchar_ch(20, 2, 12, 32, 1);
     app_lcd_dispchar_ch(36, 2, 13, 32, 1);
     app_lcd_dispchar_ch(52, 2, 14, 32, 1);
     app_lcd_dispchar_ch(68, 2, 15, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     break;
   case UI_KEYVAL_POWER_SHORT:
     ui->curpageidx = UI_PAGE_IDX_ELECTRICALDH;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add < 324)
       {
         add++;
       }
       else
       {
         add = 1;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add <= 314)
       {
         add = add + 10;
       }
     }
     else
     {
       if (add <= 224)
       {
         add = add + 100;
       }
     }
     break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add > 1)
       {
         add--;
       }
       else
       {
         add = 324;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add > 10)
       {
         add = add - 10;
       }
     }
     else
     {
       if (add > 100)
       {
         add = add - 100;
       }
     }

     break;
   case UI_KEYVAL_WRITE_SHORT:
     progbar = 0;
     ui->cursoridx = -1;
     keynumcursoridx = 0;
     if ((add > 0) && (app_loop_open() == 1))
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           // 显示进度
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_soundermodeset(mode, add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       vTaskDelay(500);
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_READ_SHORT:
     progbar=0;
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     if (app_loop_open() == 1)
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);

           res = app_loop_soundermodeget(mode, &add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_SHORT_UI:
     ui_lcd_showshort(1);
     app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     ui_lcd_showshort(0);
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     keynum = keyval - UI_KEYVAL_0_SHORT;
     if (ui->cursoridx == -1)
     {
       if (keynumcursoridx == 0)
       {
         add = 0;
       }
       if (keynumcursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         keynumcursoridx = 1;
       }
       else if (keynumcursoridx == 1)
       {
         i = (add % 10) * 10 + keynum;
         keynumcursoridx = 2;
       }
       else if (keynumcursoridx == 2)
       {
         i = add / 10 * 100 + add % 10 * 10 + keynum;

         keynumcursoridx = 0;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
     }
     else
     {
       keynumcursoridx = 0;
       if (ui->cursoridx == 0)
       {
         i = (add / 10 * 10) + keynum;
         ui->cursoridx = 2;
       }
       else if (ui->cursoridx == 1)
       {
         i = (add / 100) * 100 + keynum * 10 + (add % 10);
         ui->cursoridx = ui->cursoridx - 1;
       }
       else if (ui->cursoridx == 2)
       {
         i = keynum * 100 + add % 100;
         ui->cursoridx = ui->cursoridx - 1;
       }
       if ((i <= 324) && (i > 0))
       {
         add = i;
       }
     }
     break;
   case UI_KEYVAL_LEFT_SHORT:
     if (ui->cursoridx < 2)
     {
       ui->cursoridx++;
     }
     else
     {
       ui->cursoridx = 0;
     }
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if (ui->cursoridx > 0)
     {
       ui->cursoridx--;
     }
     else
     {
       ui->cursoridx = 2;
     }
     break;
   default:
     break;
   }
   ui_lcd_seladdresscur(add, ui->cursoridx);
}

void ui_lcd_page_idx_electrical(ui_t *ui, ui_keyval_e keyval)
{
   static int add,addbk;
   uint8_t buff[16];
   int i,j;
   int res;
   uint8_t progbar=0;
   int keynum=0;
   static loopmode_e mode = LOOPMODE_9000;
   static int keynumcursoridx;
   if (addbk != add)
   {
    ui_lcd_showresult(2);
    addbk = add;
   }

   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     ui->cursoridx=-1;
     keynumcursoridx = 0;
     add = 1000;
     app_lcd_clear();
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<电气火灾>
     app_lcd_dispchar_ch(20, 2, 16, 32, 1);
     app_lcd_dispchar_ch(36, 2, 17, 32, 1);
     app_lcd_dispchar_ch(52, 2, 18, 32, 1);
     app_lcd_dispchar_ch(68, 2, 19, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     break;
   case UI_KEYVAL_POWER_SHORT:
     ui->curpageidx = UI_PAGE_IDX_MULTIFUN;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add <1000)
       {
         add++;
       }
       else
       {
         add = 1;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add <= 990)
       {
         add = add + 10;
       }
       else
       {
         add = 1000;
       }
     }
     else if(ui->cursoridx == 2)
     {
       if (add <= 900)
       {
         add = add + 100;
       }
       else
       {
         add = 1000;
       }

     }
     else if(ui->cursoridx == 3)
     {
      if (add < 1000)
       {
         add = 1000;
       }
     }
     break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     if ((ui->cursoridx == -1) || (ui->cursoridx == 0))
     {
       if (add > 1)
       {
         add--;
       }
       else
       {
         add = 1000;
       }
     }
     else if (ui->cursoridx == 1)
     {
       if (add > 10)
       {
         add = add - 10;
       }
     }
     else if(ui->cursoridx == 2)
     {
       if (add > 100)
       {
         add = add - 100;
       }
     }
     else if(ui->cursoridx == 3)
     {
      
        add = 1;
       
     }

     break;
   case UI_KEYVAL_WRITE_SHORT:
     progbar = 0;
     ui->cursoridx = -1;
     keynumcursoridx = 0;
     if ((add > 0) && (app_loop_open() == 1))
     {
        for (j = 0; j < 2; j++)
        {
         for (i = 0; i < 5; i++)
         {
           // 显示进度
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_electricaldhset(mode, add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
        }
        vTaskDelay(500);
        app_loop_close();
     }
     else
     {
        ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_READ_SHORT:
     progbar = 0;
     ui->cursoridx = -1;
     keynumcursoridx = 0;
     if (app_loop_open() == 1)
     {
        for (j = 0; j < 2; j++)
        {
         for (i = 0; i < 5; i++)
         {
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);

           res = app_loop_electricaldhget(mode, &add);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
        }
        app_loop_close();
     }
     else
     {
        ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_SHORT_UI:
     ui_lcd_showshort(1);
     app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     ui_lcd_showshort(0);
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     keynum = keyval - UI_KEYVAL_0_SHORT;
     if (ui->cursoridx == -1)
     {
        if (keynumcursoridx == 0)
        {
         add = 0;
        }
        if (keynumcursoridx == 0)
        {
         i = (add / 10 * 10) + keynum;
         keynumcursoridx = 1;
        }
        else if (keynumcursoridx == 1)
        {
         i = (add % 10) * 10 + keynum;
         keynumcursoridx = 2;
        }
        else if (keynumcursoridx == 2)
        {
         i = add / 10 * 100 + add % 10 * 10 + keynum;
         keynumcursoridx = 3;
        }
        else if (keynumcursoridx == 3)
        {
         i = add / 100 * 1000 + add % 100 / 10 * 100 + keynum;
         keynumcursoridx = 0;
        }
        if ((i <= 1000) && (i > 0))
        {
         add = i;
        }
        else
        {
         keynumcursoridx =0;
        }
     }
     else
     {
        keynumcursoridx = 0;
        if (ui->cursoridx == 0)
        {
         i = (add / 10 * 10) + keynum;
         ui->cursoridx = 3;
        }
        else if (ui->cursoridx == 1)
        {
         i = (add / 100) * 100 + keynum * 10 + (add % 10);
         ui->cursoridx = ui->cursoridx - 1;
        }
        else if (ui->cursoridx == 2)
        {
         i = keynum * 100 + add % 100;
         ui->cursoridx = ui->cursoridx - 1;
        }
        else if (ui->cursoridx == 3)
        {
         i = keynum * 1000 + add % 1000;
         ui->cursoridx = ui->cursoridx - 1;
        }
        if ((i <= 1000) && (i > 0))
        {
         add = i;
        }
        else
        {
         ui->cursoridx = -1;
        }
     }
     break;
   case UI_KEYVAL_LEFT_SHORT:
     if (ui->cursoridx < 3)
     {
       ui->cursoridx++;
     }
     else
     {
       ui->cursoridx = 0;
     }
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if (ui->cursoridx > 0)
     {
       ui->cursoridx--;
     }
     else
     {
       ui->cursoridx = 3;
     }
     break;
   default:
     break;
   }
   ui_lcd_selelectricalcur(add, ui->cursoridx);
}


void ui_lcd_page_idx_multifun(ui_t *ui, ui_keyval_e keyval)
{
   static uint8_t fun;//功能码
   static uint16_t para;//参数
   int add;
   uint8_t buff[16];
   int i,j;
   int res;
   uint8_t progbar=0;
   int keynum=0;
   static loopmode_e mode = LOOPMODE_EPC;
   static uint8_t numflag=1;//数字输入法
   static uint32_t blacklistpass;
   static uint8_t blacklistenble,blacklistcursoridx;
   switch (keyval)
   {
   case UI_KEYVAL_INIT_UI: // 建立界面
     blacklistpass = 0;
     blacklistcursoridx = 0;
     blacklistenble = 1;

     ui->cursoridx=-1;
     fun = 0x01;
     para = 0x0001;
     numflag=1;
     app_lcd_clear();
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 0, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(0, i, 1);
     }
     //-------------------
     for (i = 0; i < MAX_COLUMN; i++)
     {
       app_lcd_drawpoint(i, 63, 1);
     }
     //|
     //|
     //|
     //|
     for (i = 0; i < MAX_ROW; i++)
     {
       app_lcd_drawpoint(127, i, 1);
     }
     //<多功能编码>
     app_lcd_dispchar_ch(20, 2, 20, 32, 1);
     app_lcd_dispchar_ch(36, 2, 21, 32, 1);
     app_lcd_dispchar_ch(52, 2, 22, 32, 1);
     app_lcd_dispchar_ch(68, 2, 23, 32, 1);
     app_lcd_dispchar_ch(84, 2, 24, 32, 1);

     //<功能码:>
     app_lcd_dispchar_ch(1, 20, 25, 32, 1);
     app_lcd_dispchar_ch(17, 20, 26, 32, 1);
     app_lcd_dispchar_ch(33, 20, 27, 32, 1);
     app_lcd_dispchar_ch(49, 20, 30, 32, 1);

     
     
     //<参数:>
     app_lcd_dispchar_ch(1, 42, 28, 32, 1);
     app_lcd_dispchar_ch(17, 42, 29,32, 1);
     app_lcd_dispchar_ch(33, 42, 30, 32, 1);
     app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);

     //<输入法>
     if (numflag == 0)
     {
       app_lcd_dispchar_str(1, 2, "abc", 12, 0);
     }
     else
     {
       app_lcd_dispchar_str(1, 2, "123", 12, 0);
     }
     break;
   case UI_KEYVAL_POWER_SHORT:
     ui->curpageidx = UI_PAGE_IDX_CODER;
     ui_lcd_postkeyval(ui,UI_KEYVAL_INIT_UI);
     break;
   case UI_KEYVAL_OK_SHORT://切换输入法
     if (blacklistenble == 1)
     {
       if (numflag == 1)
       {
         numflag = 0;
         app_lcd_dispchar_str(1, 2, "abc", 12, 0);
       }
       else
       {
         numflag = 1;
         app_lcd_dispchar_str(1, 2, "123", 12, 0);
       }
     }
     else
     {
       if (numflag == 1)
       {
         numflag = 0;
         app_lcd_dispchar_str(1, 2, "abc", 12, 1);
       }
       else
       {
         numflag = 1;
         app_lcd_dispchar_str(1, 2, "123", 12, 1);
       }
     }
     blacklistpass = 0;
     blacklistcursoridx = 0;
     break;
   case UI_KEYVAL_UP_LONG:
   case UI_KEYVAL_UP_SHORT:
     blacklistpass = 0;
     blacklistcursoridx = 0;
     switch (ui->cursoridx)
     {
     case 0:
       if (fun < 255)
       {
         fun++;
       }
       else
       {
         fun = 1;
       }
       break;
     case 1:
       if (fun <= 239)
       {
         fun = fun + 16;
       }
       else
       {
         fun = 0x01;
       }
       break;
     case 2:
       if (para < 0xffff)
       {
         para = para + 1;
       }
       else
       {
         para = 0x0001;
       }
       break;
      case 3:
       if (para <= 0xffef)
       {
         para = para + 16;
       }
       else
       {
         para = 0x0001;
       }
       break;
      case 4:
       if (para <= 0xfeff)
       {
         para = para + 256;
       }
       else
       {
         para = 0x0001;
       }
       break;
       case 5:
       if (para <= 0xefff)
       {
         para = para + 4096;
       }
       else
       {
         para = 0x0001;
       }
       break;
     default:
       break;
     }
		 break;
   case UI_KEYVAL_DOWN_LONG:
   case UI_KEYVAL_DOWN_SHORT:
     blacklistpass = 0;
     blacklistcursoridx = 0;
     switch (ui->cursoridx)
     {
     case 0:
       if (fun > 1)
       {
         fun--;
       }
       else
       {
         fun = 255;
       }
       break;
     case 1:
       if (fun > 16)
       {
         fun = fun - 16;
       }
       else
       {
         fun = 255;
       }
       break;
     case 2:
       if (para > 0x0001)
       {
         para = para - 1;
       }
       else
       {
         para = 0xffff;
       }
       break;
      case 3:
       if (para > 0x10)
       {
         para = para - 0x10;
       }
       else
       {
         para = 0xffff;
       }
       break;
      case 4:
       if (para > 0x0100)
       {
         para = para - 0x0100;
       }
       else
       {
         para = 0xffff;
       }
       break;
       case 5:
       if (para > 0x1000)
       {
         para = para - 0x1000;
       }
       else
       {
         para = 0xffff;
       }
       break;
     default:
       break;
     }
     break;
   case UI_KEYVAL_WRITE_SHORT:
     progbar=0;
     ui->cursoridx=-1;
     blacklistpass = 0;
     blacklistcursoridx = 0;
     if (app_loop_open() == 1)
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           // 显示进度
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_mulfunset(mode, fun, para,blacklistenble);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       vTaskDelay(500);
       app_loop_close();
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_READ_SHORT:
     progbar=0;
     ui->cursoridx=-1;
     blacklistpass = 0;
     blacklistcursoridx = 0;
     if (app_loop_open() == 1)
     {
       for (j = 0; j < 2; j++)
       {
         for (i = 0; i < 5; i++)
         {
           progbar = 10 * i + j * 50;
           ui_lcd_showprogbar(progbar, 1);
           res = app_loop_mulfunget(mode, fun, &para);
           if (res == 1)
           {
             app_beep_successtone_1();
             break;
           }
         }
         if (res == 0)
         {
           if (j > 0)
           {
             ui_lcd_showresult(0);
             app_beep_errortone();
           }
           else
           {
             if (mode == LOOPMODE_9000)
             {
               mode = LOOPMODE_EPC;
             }
             else
             {
               mode = LOOPMODE_9000;
             }
           }
         }
         else
         {
           ui_lcd_showprogbar(100, 1);
           vTaskDelay(100);
           ui_lcd_showresult(1);
           break;
         }
       }
       app_loop_close();
      
     }
     else
     {
       ui_lcd_showresult(0);
     }
     break;
   case UI_KEYVAL_0_SHORT:
   case UI_KEYVAL_1_SHORT:
   case UI_KEYVAL_2_SHORT:
   case UI_KEYVAL_3_SHORT:
   case UI_KEYVAL_4_SHORT:
   case UI_KEYVAL_5_SHORT:
   case UI_KEYVAL_6_SHORT:
   case UI_KEYVAL_7_SHORT:
   case UI_KEYVAL_8_SHORT:
   case UI_KEYVAL_9_SHORT:
     if (ui->cursoridx == -1)
     {
       if (blacklistenble == 1)
       {
         if (numflag == 1) // 数字输入法
         {
           keynum = keyval - UI_KEYVAL_0_SHORT;
           if (blacklistcursoridx < 8)
           {
             blacklistpass = blacklistpass + keynum * pow(10, blacklistcursoridx);
             blacklistcursoridx++;
             msg("blacklistpass=%d,blacklistcursoridx=%d\r\n",blacklistpass,blacklistcursoridx);
             if (blacklistpass == 13228562)
             {
               blacklistenble = 0;
               app_lcd_dispchar_str(1, 2, "123", 12, 1);
               
             }
           }
         }
       }
     }
     else
     {
       blacklistpass = 0;
       blacklistcursoridx = 0;
       if (numflag == 1) // 数字输入法
       {
         keynum = keyval - UI_KEYVAL_0_SHORT;
       }
       else // 字母输入法
       {
         if ((keyval <= UI_KEYVAL_6_SHORT) && (keyval > UI_KEYVAL_0_SHORT))
         {
           keynum = 0x0a + (keyval - UI_KEYVAL_1_SHORT);
         }
         else
         {
           break;
         }
       }
       if (ui->cursoridx <= 1)
       {
         if (ui->cursoridx == 0)
         {
           i = (fun / 16) * 16 + keynum;
         }
         else if (ui->cursoridx == 1)
         {
           i = keynum * 16 + (fun % 16);
         }
         if ((i <= 255) && (i > 0))
         {
           fun = i;
         }
       }
       else
       {
         if (ui->cursoridx == 2)
         {
           i = (para / 16) * 16 + keynum;
         }
         else if (ui->cursoridx == 3)
         {
           i = (para / 256) * 256 + keynum * 16 + para % 16;
         }
         else if (ui->cursoridx == 4)
         {
           i = (para / 4096) * 4096 + keynum * 256 + para % 256;
         }
         else if (ui->cursoridx == 5)
         {
           i = keynum * 4096 + para % 4096;
         }
         if ((i <= 0xffff) && (i > 0))
         {
           para = i;
         }
       }
     }
     break;
   case UI_KEYVAL_SHORT_UI:
     ui_lcd_showshort(1);
     app_beep_errortone();
     break;
   case UI_KEYVAL_SHORT_EM_UI:
     ui_lcd_showshort(0);
     break;
   case UI_KEYVAL_LEFT_SHORT:
     if (ui->cursoridx < 5)
     {
       ui->cursoridx++;
     }
     else
     {
       ui->cursoridx = 0;
     }
     blacklistpass = 0;
     blacklistcursoridx = 0;
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     if (ui->cursoridx > 0)
     {
       ui->cursoridx--;
     }
     else
     {
       ui->cursoridx = 5;
     }
     blacklistpass = 0;
     blacklistcursoridx = 0;
     break;
   default:
     break;
   }
   ui_lcd_selmultifuncur(fun,para, ui->cursoridx);
}
void ui_lcd_testkeyshow(ui_keyval_e keyval)
{
   char  buff[16];
   switch (keyval)
   {
   case UI_KEYVAL_READ_SHORT:
     app_lcd_dispchar_ch(48, 30, 33, 32, 1);//读
     break;
   case UI_KEYVAL_WRITE_SHORT:
     app_lcd_dispchar_ch(48, 30, 34, 32, 1);//写
     break;
   case UI_KEYVAL_LEFT_SHORT:
     app_lcd_dispchar_ch(48, 30, 35, 32, 1);//←
     break;
   case UI_KEYVAL_RIGHT_SHORT:
     app_lcd_dispchar_ch(48, 30, 36, 32, 1);//→
     break;
   case UI_KEYVAL_UP_SHORT:
     app_lcd_dispchar_ch(48, 30, 37, 32, 1);//↑
     break;
   case UI_KEYVAL_DOWN_SHORT:
     app_lcd_dispchar_ch(48, 30, 38, 32, 1);//↓
     break;
   case UI_KEYVAL_OK_SHORT:
     app_lcd_dispchar_str(48, 30, "OK", 16, 1);//OK
     break;
   case UI_KEYVAL_POWER_SHORT:
     app_lcd_dispchar_str(48, 30, "TAB", 16, 1);//TAB
     break;
   case UI_KEYVAL_0_SHORT:
     app_lcd_dispchar_str(48, 30, "0  ", 16, 1);//0
     break;
   case UI_KEYVAL_1_SHORT:
     app_lcd_dispchar_str(48, 30, "1  ", 16, 1);//1
     break;
   case UI_KEYVAL_2_SHORT:
     app_lcd_dispchar_str(48, 30, "2  ", 16, 1);//2
     break;
   case UI_KEYVAL_3_SHORT:
     app_lcd_dispchar_str(48, 30, "3  ", 16, 1);//3
     break;
   case UI_KEYVAL_4_SHORT:
     app_lcd_dispchar_str(48, 30, "4  ", 16, 1);//4
     break;
   case UI_KEYVAL_5_SHORT:
     app_lcd_dispchar_str(48, 30, "5  ", 16, 1);//5
     break;
   case UI_KEYVAL_6_SHORT:
     app_lcd_dispchar_str(48, 30, "6  ", 16, 1);//6
     break;
   case UI_KEYVAL_7_SHORT:
     app_lcd_dispchar_str(48, 30, "7  ", 16, 1);//7
     break;
   case UI_KEYVAL_8_SHORT:
     app_lcd_dispchar_str(48, 30, "8  ", 16, 1);//8
     break;
   case UI_KEYVAL_9_SHORT:
     app_lcd_dispchar_str(48, 30, "9  ", 16, 1);//9
     break;

   default:
     break;
   }
}
void ui_lcd_page_idx_testfun(ui_t *ui, ui_keyval_e keyval)
{
   static uint8_t teststep = 0;
   static ui_keyval_e testkeyval=UI_KEYVAL_READ_SHORT;
   static ui_keyval_e testkeyvalbk=UI_KEYVAL_NON;
   uint8_t x, y;
   static TickType_t starttick;
   int add;
   uint16_t batterval=0;
   char buff[32];
   static int pointtestaddress=1;
   int res;
   if(keyval)
   {
    app_beep_keytone();
   }
   switch (teststep)
   {
   case 0: // 屏幕测试
     app_lcd_clear();
     app_lcd_dispchar_str(20, 0, "1.LCD TEST", 16, 1);
     vTaskDelay(100);
     for (y = 0; y < MAX_ROW; y++)
     {
       for (x = 0; x < MAX_COLUMN; x++)
       {
         app_lcd_drawpoint(x, y, 1);
       }
       vTaskDelay(20);
     }
     vTaskDelay(3000);
     teststep = 1;
     break;
   case 1: // 背光测试
     app_lcd_clear();
     app_lcd_dispchar_str(20, 0, "2.BL TEST", 16, 1);
     for (x = 0; x < 10; x++)
     {
      app_lcd_setbllevel(x);
      sprintf(buff,"Level:%d",x);
      app_lcd_dispchar_str(10, 20, buff, 16, 1);
      vTaskDelay(500);
     }
     app_lcd_setbllevel(1);
     teststep = 2;
     break;
   case 2: // 蜂鸣器测试
     app_lcd_clear();
     app_lcd_dispchar_str(20, 0, "3.BUZ TEST", 16, 1);
     app_beep_start(50, 10, 5);	/* 鸣叫500ms，停100ms， 5次 */
     vTaskDelay(5000);
     teststep = 3;
     pointtestaddress=1;
     starttick = 0;
     app_lcd_clear();
     break;
  
   case 3: //总线测试
     app_lcd_dispchar_str(20, 0, "4.LOOP TEST", 16, 1);
     if (xTaskGetTickCount() - starttick > 500)
     {
      if (app_loop_open() == 1)
      {
         starttick = xTaskGetTickCount();
         sprintf(buff, "Read %d:", pointtestaddress);
         app_lcd_dispchar_str(20, 16+(pointtestaddress-1)*16, buff, 16, 1);
         res = (uint16_t)app_loop_pointtest(LOOPMODE_9000, pointtestaddress);
         if (res)
         {
           sprintf(buff, "Read %d:OK", pointtestaddress);
           app_lcd_dispchar_str(20, 16+(pointtestaddress-1)*16, buff, 16, 1);
           pointtestaddress++;
         }
         if (pointtestaddress > 3)
         {
           vTaskDelay(500);
           teststep = 4;
           pointtestaddress = 0;
           app_lcd_clear();
         }
      }
      else
      {
         app_lcd_dispchar_str(20, 16, "LOOP Short", 16, 1);
      }
     }

     if (keyval == UI_KEYVAL_READ_SHORT)
     {
      teststep = 4;
      pointtestaddress = 0;
      app_lcd_clear();
     }
     break;
   case 4: // 电池测试
     app_lcd_dispchar_str(0, 0, "5.BATTER TEST", 16, 1);
     batterval = 0;
     if (xTaskGetTickCount() - starttick > 500)
     {
      batterval = bsp_powerctrl_getbattervalue();
      batterval = batterval * 3300 * 2 / 4095;
      sprintf(buff, "Vol:%4dmV", batterval);
      app_lcd_dispchar_str(10, 30, buff, 16, 1);

      if (batterval > 2500)
      {
         pointtestaddress++;
      }
      else
      {
         pointtestaddress = 0;
      }
     }
     if ((pointtestaddress > 10) || (keyval == UI_KEYVAL_READ_SHORT)) // 2.6V,电量
     {
      vTaskDelay(500);
      teststep = 5;
      pointtestaddress = 0;
      starttick = 0;
      app_lcd_clear();
     }
     break;
   case 5: // 外置电源测试
     app_lcd_dispchar_str(0, 0, "6.POWER TEST", 16, 1);
     if (xTaskGetTickCount() - starttick > 100)
     {
      res = (uint16_t)app_powerctrl_getbatterlevel();
      msg("res=%d\r\n",res);
      if (pointtestaddress == 0)
      {
        sprintf(buff, "PULL OUT:");
        app_lcd_dispchar_str(0, 16, buff,16, 1);
        if (res > 0)
        {
          pointtestaddress=1;
          sprintf(buff, "PULL OUT:OK");
          app_lcd_dispchar_str(0, 16, buff,16, 1);
          vTaskDelay(1000);
        }
        
      }
      else
      {
        sprintf(buff, "PULL IN:");
        app_lcd_dispchar_str(0, 32, buff,16, 1);
        if (res == 0)
        {
          pointtestaddress = 2;
          sprintf(buff, "PULL IN:OK");
          app_lcd_dispchar_str(0, 32, buff, 16, 1);
          vTaskDelay(2000);
          teststep = 6;
          app_lcd_clear();
        }
      }
      starttick = xTaskGetTickCount();
     }
     if(keyval == UI_KEYVAL_READ_SHORT )
     {
        teststep = 6;
        app_lcd_clear();
     }
     break;
    case 6: // 按键测试
     app_lcd_dispchar_str(20, 0, "7.KEY TEST", 16, 1);
     if(testkeyval!=testkeyvalbk)
     {
       ui_lcd_testkeyshow(testkeyval);
       testkeyvalbk = testkeyval;
     }
     if (keyval != UI_KEYVAL_NON)
     {
      if ((keyval == testkeyval) || (keyval == testkeyval + UI_KEYVAL_READ_LONG))
      {
         testkeyval++;
      }
      if ((testkeyval > UI_KEYVAL_9_SHORT) || (testkeyval > UI_KEYVAL_9_LONG))
      {
         teststep = 7;
      }
     }
     break;
    case 7://测试完成
     if (xTaskGetTickCount() - starttick > 1000)
     {
      starttick = xTaskGetTickCount();
      app_lcd_dispchar_str(20, 16, "TEST FINSH", 16, 1);
      app_lcd_dispchar_str(0, 32, "ANY KEY TO EXIT", 16, 1);
     }
     if (keyval != UI_KEYVAL_NON)
     {
      ui->curpageidx = UI_PAGE_IDX_CODER;
      ui_lcd_postkeyval(ui, UI_KEYVAL_INIT_UI);
      ui->testmodecnt = 0;
     }
     break;
   default:
     break;
   }
}

/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参: 
*	返 回 值: 
* 说    明：
*********************************************************************************************************
*/

void ui_lcd_process(ui_t *ui)
{
   ui_keyval_e keyval;
   uint8_t buff[32];
   uint32_t len, i;
   keyval = ui_lcd_getkeyval(ui);
   if ((ui->curpageidx == UI_PAGE_IDX_TESTMODE))
   {
     ui_lcd_page_idx_testfun(ui, keyval);
     // 关机处理
     if ((keyval == UI_KEYVAL_POWER_LONG))
     {

      app_lcd_clear();
      app_lcd_refresh_gram();
      bsp_lcd_bl_level(0);
      app_powerctrl_systempower(0);
      ui->curpageidx = UI_PAGE_IDX_MAX;
      ui->poweronflag = 0;
      while (1)
      {
         msg("poweroff\r\n");
         vTaskDelay(100);
      }
     }
   }
   else
   {
     if (keyval)
     {

      if (ui_lcd_isrealkeyval(keyval) == true)
      {
         // 重新点亮的，键值不处理
         if (xTaskGetTickCount() - ui->lcdlighttimer > 30000)
         {
          keyval = UI_KEYVAL_NON;
         }
         ui->lcdlighttimer = xTaskGetTickCount();
         ui->powerofftimer = xTaskGetTickCount();
         app_lcd_setbllevel(1);
         if (ui->poweronflag == 1)
         {
          if ((keyval <= UI_KEYVAL_9_SHORT) && (keyval > UI_KEYVAL_NON))
          {
             app_beep_keytone();
          }
         }
      }

      // 关机处理
      if (((keyval == UI_KEYVAL_POWER_LONG) && (ui->curpageidx != UI_PAGE_IDX_INIT)) || (keyval == UI_KEYVAL_POWEROFF_UI))
      {

         app_lcd_clear();
         app_lcd_refresh_gram();
         bsp_lcd_bl_level(0);
         app_powerctrl_systempower(0);
         ui->curpageidx = UI_PAGE_IDX_MAX;
         ui->poweronflag = 0;
         while (1)
         {
          msg("poweroff\r\n");
          vTaskDelay(100);
         }
      }

      switch (ui->curpageidx)
      {
      case UI_PAGE_IDX_INIT:
         ui_lcd_page_idx_init(ui, keyval);
         break;
      case UI_PAGE_IDX_CODER:
         ui_lcd_page_idx_coder(ui, keyval);
         break;
      case UI_PAGE_IDX_INC_CODER:
         ui_lcd_page_idx_inccoder(ui, keyval);
         break;
      case UI_PAGE_IDX_MODELSET:
         ui_lcd_page_idx_model(ui, keyval);
         break;
      case UI_PAGE_IDX_SOUNDERSET:
         ui_lcd_page_idx_sounder(ui, keyval);
         break;
      case UI_PAGE_IDX_ELECTRICALDH:
         ui_lcd_page_idx_electrical(ui, keyval);
         break;
      case UI_PAGE_IDX_MULTIFUN:
         ui_lcd_page_idx_multifun(ui, keyval);
         break;
      default:
         break;
      }
     }

     // 30后产测失效
     if ((ui->testmodecnt) && (xTaskGetTickCount() - ui->testmodecnt > 30000))
     {
      ui->testmodecnt = 0;
      msg("TestMode Stop\r\n");
     }

     // 100ms定时发送页面更新,并采集电池电压
     if (xTaskGetTickCount() - ui->lcdreflashtimer > 100)
     {
      ui->lcdreflashtimer = xTaskGetTickCount();
      ui_lcd_postkeyval(&uitcb, UI_KEYVAL_TICK_UI);
      ui->batterlevel = app_powerctrl_getbatterlevel();
      app_lcd_showpicture(104, 4, batter[ui->batterlevel], 22, 12);
     }

     if (ui->autopoweroff == 1)
     {
      // 5min不操作就关机
      if (xTaskGetTickCount() - ui->powerofftimer > 500000)
      {
         ui_lcd_postkeyval(ui, UI_KEYVAL_POWEROFF_UI);
      }
      // 30s无操作后灭屏
      if (xTaskGetTickCount() - ui->lcdlighttimer > 30000)
      {
         app_lcd_setbllevel(0);
      }
     }
     else
     {
      ui->lcdlighttimer = xTaskGetTickCount();
      ui->powerofftimer = xTaskGetTickCount();
     }
   }

   len = tty.read(buff, 32);
   for (i = 0; i < sizeof(buff); i++)
   {
   }
}
